﻿Imports Framework.Data

Public Class Settings
    Public Shared ReadOnly SetupLevel_Required As Integer = 5
    Public Shared ReadOnly Property TabKeys() As String()
        Get
            Return New String() {NetworkSettingsTabKey, MyInfoSettingsTabKey}
        End Get
    End Property
    Friend Shared ReadOnly NetworkSettingsTabKey As String = "NetNode_Network"
    Friend Shared ReadOnly MyInfoSettingsTabKey As String = "NetNode_MyInfo"

    Private Shared mSettingsRead As Boolean = False
    Private Shared mHost As String
    Private Shared mPort As Integer
    Private Shared mListen As Boolean
    Private Shared mSetupLevel As Integer = 0
    Private Shared mReconnectInterval As Integer = 600
    Private Shared mNickName As String
    Private Shared mEmail As String
    Private Shared mUniqueIdEx As String
    Private Shared mAutoConnect As Boolean
    Private Shared mTraceTraffic As Boolean
    Private Shared mThrottleDownStream As Boolean
    Private Shared mThrottleUpStream As Boolean
    Private Shared mDownStreamBandwidthLimit As Integer
    Private Shared mUpStreamBandwidthLimit As Integer
    Private Shared mUseReceiveDataMemoryBuffer As Boolean

    Public Shared ReadOnly Property Host() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mHost
        End Get
    End Property

    Public Shared Sub SetHost(ByVal value As String, ByVal transactionScope As TransactionScope)
        If mHost <> value Then
            Framework.Data.MetaData.SetValue("NetNode_Host", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mHost", value)
        End If
    End Sub
    Public Shared ReadOnly Property UniqueId() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mUniqueIdEx
        End Get
    End Property
    Friend Shared Sub SetUniqueId(ByVal value As String, ByVal transactionScope As TransactionScope)
        If mUniqueIdEx <> value Then
            Framework.Data.MetaData.SetValue("NetNode_UniqueId", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mUniqueIdEx", value)
        End If
    End Sub

    Public Shared ReadOnly Property Port() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mPort
        End Get
    End Property
    Public Shared Sub SetPort(ByVal value As Integer, ByVal transactionScope As TransactionScope)
        If mPort <> value Then
            Framework.Data.MetaData.SetValue("NetNode_Port", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mPort", value)
        End If
    End Sub
    Public Shared ReadOnly Property Listen() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mListen
        End Get
    End Property
    Friend Shared Sub SetListen(ByVal value As Boolean, ByVal transactionScope As TransactionScope)
        If mListen <> value Then
            Framework.Data.MetaData.SetValue("NetNode_Listen", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mListen", value)
        End If
    End Sub
    Public Shared ReadOnly Property AutoConnect() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mAutoConnect
        End Get
    End Property
    Public Shared Sub SetAutoConnect(ByVal value As Boolean, ByVal transactionScope As TransactionScope)
        If mAutoConnect <> value Then
            Framework.Data.MetaData.SetValue("NetNode_AutoConnect", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mAutoConnect", value)
        End If
    End Sub
    Public Shared ReadOnly Property TraceTraffic() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mTraceTraffic
        End Get
    End Property
    Friend Shared Sub SetTraceTraffic(ByVal value As Boolean, ByVal transactionScope As TransactionScope)
        If mTraceTraffic <> value Then
            Framework.Data.MetaData.SetValue("NetNode_TraceTraffic", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mTraceTraffic", value)
        End If
    End Sub

    Public Shared ReadOnly Property SetupLevel_Saved() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mSetupLevel
        End Get
    End Property
    Friend Shared Sub SetSetupLevel_Saved(ByVal value As Integer, ByVal transactionScope As TransactionScope)
        If mSetupLevel <> value Then
            Framework.Data.MetaData.SetValue("NetNode_SetupLevel", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mSetupLevel", value)
        End If
    End Sub

    Public Shared ReadOnly Property ReconnectInterval() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mReconnectInterval
        End Get
    End Property
    Friend Shared Sub SetReconnectInterval(ByVal value As Integer, ByVal transactionScope As TransactionScope)
        If mSetupLevel <> value Then
            Framework.Data.MetaData.SetValue("NetNode_ReconnectInterval", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mReconnectInterval", value)
        End If
    End Sub

    Public Shared ReadOnly Property Nickname() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mNickName
        End Get
    End Property
    Friend Shared Sub SetNickname(ByVal value As String, ByVal transactionScope As TransactionScope)
        If mNickName <> value Then
            Framework.Data.MetaData.SetValue("NetNode_Nickname", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mNickName", value)
        End If
    End Sub

    Public Shared ReadOnly Property Email() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mEmail
        End Get
    End Property
    Friend Shared Sub SetEmail(ByVal value As String, ByVal transactionScope As TransactionScope)
        If mEmail <> value Then
            Framework.Data.MetaData.SetValue("NetNode_Email", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mEmail", value)
        End If
    End Sub

    Public Shared ReadOnly Property ThrottleDownStream() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mThrottleDownStream
        End Get
    End Property
    Friend Shared Sub SetThrottleDownStream(ByVal value As Boolean, ByVal transactionScope As TransactionScope)
        If mThrottleDownStream <> value Then
            Framework.Data.MetaData.SetValue("NetNode_ThrottleDownStream", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mThrottleDownStream", value)
        End If
    End Sub
    Public Shared ReadOnly Property ThrottleUpStream() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mThrottleUpStream
        End Get
    End Property
    Friend Shared Sub SetThrottleUpStream(ByVal value As Boolean, ByVal transactionScope As TransactionScope)
        If mThrottleUpStream <> value Then
            Framework.Data.MetaData.SetValue("NetNode_ThrottleUpStream", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mThrottleUpStream", value)
        End If
    End Sub

    ''' <summary>
    ''' Limit in kilobytes per second
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property DownStreamBandwidthLimitBytesPerSecond() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mDownStreamBandwidthLimit
        End Get
    End Property
    Friend Shared Sub SetDownStreamBandwidthLimitBytesPerSecond(ByVal value As Integer, ByVal transactionScope As TransactionScope)
        If mDownStreamBandwidthLimit <> value Then
            Framework.Data.MetaData.SetValue("NetNode_DownStreamBandwidthLimitBytesPerSec", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mDownStreamBandwidthLimit", value)
        End If
    End Sub
    ''' <summary>
    ''' Limit in bytes per second
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property UpStreamBandwidthLimitBytesPerSecond() As Integer
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mUpStreamBandwidthLimit
        End Get
    End Property
    Friend Shared Sub SetUpStreamBandwidthLimitBytesPerSecond(ByVal value As Integer, ByVal transactionScope As TransactionScope)
        If mUpStreamBandwidthLimit <> value Then
            Framework.Data.MetaData.SetValue("NetNode_UpStreamBandwidthLimitBytesPerSec", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mUpStreamBandwidthLimit", value)
        End If
    End Sub

    Public Shared ReadOnly Property UseReceiveDataMemoryBuffer() As Boolean
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mUseReceiveDataMemoryBuffer
        End Get
    End Property
    Friend Shared Sub SetUseReceiveDataMemoryBuffer(ByVal value As Boolean, ByVal transactionScope As TransactionScope)
        If mListen <> value Then
            Framework.Data.MetaData.SetValue("NetNode_UseReceiveDataMemoryBuffer", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mUseReceiveDataMemoryBuffer", value)
        End If
    End Sub


    Private Shared mRSAProvider As System.Security.Cryptography.RSACryptoServiceProvider
    Public Shared ReadOnly Property RSAProvider() As System.Security.Cryptography.RSACryptoServiceProvider
        Get
            Return mRSAProvider
        End Get
    End Property
    Public Shared Sub SetupRSAProvider()
        Dim clsRSAProvider As New System.Security.Cryptography.RSACryptoServiceProvider(1024)
        Dim sRsaXml As String = clsRSAProvider.ToXmlString(True)
        Using t As New TransactionScope
            Framework.Data.MetaData.SetValue("NetNode_RSAKey", sRsaXml, t)
            t.SetClassFieldValue(GetType(Settings), "mRSAProvider", clsRSAProvider)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub SetupUniqueId()
        Using t As New TransactionScope
            SetUniqueId(Framework.Misc.GenerateUniqueId, t)
            t.Complete()
        End Using
    End Sub

    Private Shared Sub LoadSettings()
        mHost = Framework.Data.MetaData.GetValueAsString("NetNode_Host", Nothing)
        mPort = Framework.Data.MetaData.GetValueAsInteger("NetNode_Port", 0)
        mListen = Framework.Data.MetaData.GetValueAsBoolean("NetNode_Listen", False)
        mAutoConnect = Framework.Data.MetaData.GetValueAsBoolean("NetNode_AutoConnect", False)
        mReconnectInterval = Framework.Data.MetaData.GetValueAsInteger("NetNode_ReconnectInterval", 600)
        mSetupLevel = Framework.Data.MetaData.GetValueAsInteger("NetNode_SetupLevel", 0)
        mNickName = Framework.Data.MetaData.GetValueAsString("NetNode_Nickname", System.Environment.UserName & "@" & System.Environment.MachineName)
        mEmail = Framework.Data.MetaData.GetValueAsString("NetNode_Email", Nothing)
        mUniqueIdEx = Framework.Data.MetaData.GetValueAsString("NetNode_UniqueId", Nothing)
        Dim sRSAXml As String = Framework.Data.MetaData.GetValueAsString("NetNode_RSAKey", Nothing)
        If sRSAXml IsNot Nothing Then
            mRSAProvider = New System.Security.Cryptography.RSACryptoServiceProvider
            mRSAProvider.FromXmlString(sRSAXml)
        End If
        mTraceTraffic = Framework.Data.MetaData.GetValueAsBoolean("NetNode_TraceTraffic", False)
        mThrottleDownStream = Framework.Data.MetaData.GetValueAsBoolean("NetNode_ThrottleDownStream", False)
        mThrottleUpStream = Framework.Data.MetaData.GetValueAsBoolean("NetNode_ThrottleUpStream", False)
        mUpStreamBandwidthLimit = Framework.Data.MetaData.GetValueAsInteger("NetNode_UpStreamBandwidthLimitBytesPerSec", 1024 * 1024)
        mDownStreamBandwidthLimit = Framework.Data.MetaData.GetValueAsInteger("NetNode_UpStreamBandwidthLimitBytesPerSec", 1024 * 10 * 1024)
        mUseReceiveDataMemoryBuffer = Framework.Data.MetaData.GetValueAsBoolean("NetNode_UseReceiveDataMemoryBuffer", False)

        mSettingsRead = True
    End Sub

End Class
